<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;

class GameController extends AdminBaseController
{
    /**
     * 游戏列表
     */
    public function gameList()
    {
        $list=Db::name('game')->paginate(20);
        $page = $list->render();
        $list_view=[];
        foreach ($list as $v){
            $v['win_probability']=(string)($v['win_probability']*100).'%';
            $str_multiple_list='';
            foreach(json_decode($v['multiple_list'],true) as &$v1){
                $str_multiple_list.=$v1."倍 ";
            }
            unset($v1);
            $v['multiple_list']=$str_multiple_list;
            $v['game_ename']=$v['game_ename']?$v['game_ename']:'无';
            $list_view[]=$v;
        }
        $this->assign([
            'list'=>$list_view,
            'page'=>$page
        ]);
        return $this->fetch();
    }
    /**
     *  编辑界面
     * */
    public function edit()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $gameInfo = DB::name('game')->where(["id" => $id])->find();
        $multiple_list='';
        foreach(json_decode($gameInfo['multiple_list'],true) as $v){
            $multiple_list.=$v.'.';
        }
        $gameInfo['multiple_list']=substr($multiple_list, 0, -1);
        $this->assign($gameInfo);
        return $this->fetch();
    }
    /**
     *  编辑提交
     * */
    public function editPost()
    {
        if ($this->request->isPost()) {
            $result = $this->validate($this->request->param(), 'Game.edit');
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                $multiple_list=explode('.',trim($_POST['multiple_list']));
                $_POST['multiple_list']=json_encode($multiple_list);
                $_POST['updated_time']=date('Y-m-d H:i:s',time());
                $result = DB::name('game')->cache('game_list')->update($_POST);
                if ($result !== false) {
                    $this->success("保存成功！");
                } else {
                    $this->error("保存失败！");
                }
            }

        }
    }
    /**
     *  添加界面
     * */
    public function add()
    {
        return $this->fetch();
    }
    /**
     *  添加提交
     * */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $result = $this->validate($this->request->param(), 'Game.edit');
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                $multiple_list=explode('.',trim($_POST['multiple_list']));
                $_POST['multiple_list']=json_encode($multiple_list);
                $_POST['created_time']=date('Y-m-d H:i:s',time());
                $result = DB::name('game')->cache('game_list')->insert($_POST);
                if ($result !== false) {
                    $this->success("保存成功！");
                } else {
                    $this->error("保存失败！");
                }
            }
        }
        return $this->fetch();
    }
    /**
     *  删除提交
     * */
    public function delete()
    {
        $id=$this->request->param('id');
        $re=DB::name('game')-> where("id",$id) ->cache('game_list')->delete();
        if($re!==false){
            $this->success("删除成功！");
        }else{
            $this->error("删除失败！");
        }
    }
}
